/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sunspotworld.protocols;

import com.sun.spot.peripheral.radio.IProtocolManager;
import com.sun.spot.peripheral.radio.LowPanHeaderInfo;
import org.sunspotworld.sensortasks.TimeKeeper;

/**
 *
 * @author me
 */
public class TimeProtocol implements IProtocolManager {
    public static final byte PROTOCOL_NUM = MyProtocols.TIME;
    
    public void processIncomingData(byte[] arg0, LowPanHeaderInfo arg1) {
        processTime(arg0, arg1);
    }
    
    public static void processTime(byte[] arg0, LowPanHeaderInfo arg1) {
        TimeKeeper.getInstance().updateTime(byteArrayToLong(arg0), arg1.timestamp);
    }
    
    private static long byteArrayToLong(byte[] b) {
        return ((long)b[0] << 56)
                + (((long)b[1] & 0xFF) << 48)
                + (((long)b[2] & 0xFF) << 40)
                + (((long)b[3] & 0xFF) << 32)
                + (((long)b[4] & 0xFF) << 24)
                + (((long)b[5] & 0xFF) << 16)
                + (((long)b[6] & 0xFF) << 8)
                + ((long)b[7] & 0xFF);
    }
}
